package com.cskaoyan.service;

import com.cskaoyan.bean.MarketAddress;
import com.cskaoyan.bean.MarketAddressExample;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketAddressMapper;
import com.cskaoyan.util.StringUtil;
import com.cskaoyan.util.TransactionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.List;

public class AddressServiceImpl implements AddressService {
    @Override
    public CommonData<MarketAddress> query(Integer page, Integer limit, String sort, String order, String username, Integer id) {
        MarketAddressExample example = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = example.createCriteria();

        if (id != null) {
            criteria.andUserIdEqualTo(id);
        }
        if (!StringUtil.isEmpty(username)) {
            criteria.andNameLike("%"+username+"%");
        }

        criteria.andDeletedEqualTo(false);

        if (!StringUtil.isEmpty(sort) && !StringUtil.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, limit);

        MarketAddressMapper marketAddressMapper = TransactionUtil.getMapper(MarketAddressMapper.class);
        PageHelper.startPage(page, limit);
        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(example);
        return CommonData.data(new PageInfo(marketAddresses));
    }
}

